package fregat.base.ui;

import javax.swing.*;
import java.awt.*;

/**
 * Aligns all attributes of the {@link at.ecolog.base.gui.ListElement} on a panel.
 *
 * @author Alexander Mann <br><b><i>ECO_L O G</i> &nbsp Logistiksysteme GmbH</b>
 * @version 1.0
 */
public class AttributeRenderer {

  public static void addComponents(JPanel panel, JComponent[] components) {
    GridBagConstraints gridBagConstraints = new GridBagConstraints();
    ;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
    gridBagConstraints.anchor = GridBagConstraints.WEST;

    panel.removeAll();
    for (int i = 0; i < components.length; i++) {
      JComponent component = components[i];
      gridBagConstraints.gridx = 0;
      panel.add(new JLabel(component.getName()), gridBagConstraints);
      gridBagConstraints.gridx = 1;
      panel.add(component, gridBagConstraints);
    }

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.weightx = 1;
    panel.add(new JPanel(), gridBagConstraints);


    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.weighty = 1;
    panel.add(new JPanel(), gridBagConstraints);

    panel.revalidate();
    panel.repaint();
  }

  public static void resetComponents(JPanel panel) {
    for (int i = 0; i < panel.getComponentCount(); i++) {
      Component component = panel.getComponent(i);
      if (component instanceof JTextField)
        ((JTextField) component).setText("");
      else if (component instanceof JComboBox)
        ((JComboBox) component).setSelectedIndex(0);
      else if (component instanceof JCheckBox)
        ((JCheckBox) component).setSelected(false);
    }
  }
}
